package com.uvt.agenda;

import com.uvt.EventPrototype;
import org.joda.time.DateTime;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by Cristian Holdunu on 22/01/14.
 * 00:50
 */
public class Event extends EventPrototype{
    int ID = 0;
    String name;
    String description;
    Long startDate;
    Long endDate;
    private DateTime startDateTime;
    private DateTime endDateTime;

    public Event() {
        startDateTime = DateTime.now();
        endDateTime = DateTime.now().plusHours(1);
        startDate = startDateTime.getMillis();
        endDate = endDateTime.getMillis();
    }

    public Event(String name, Long startDate) {
        this.name = name;
        this.startDate = startDate;
        this.startDateTime = new DateTime(startDate);
    }

    public Event(String name, String description, Long startDate, Long endDate) {
        this.name = name;
        this.description = description;
        this.startDate = startDate;
        this.endDate = endDate;
        this.startDateTime = new DateTime(startDate);
        this.endDateTime = new DateTime(endDate);
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getStartDate() {
        return startDate;
    }

    public void setStartDate(Long startDate) {
        this.startDate = startDate;
        this.startDateTime = new DateTime(startDate);
    }

    public Long getEndDate() {
        return endDate;
    }

    public void setEndDate(Long endDate) {
        this.endDate = endDate;
        this.endDateTime = new DateTime(endDate);
    }

    public int getStartMonth(){
        return startDateTime.getMonthOfYear();
    }
    public int getEndMonth(){
        return endDateTime.getMonthOfYear();
    }
    public int getStartDay(){
        return startDateTime.getDayOfMonth();
    }
    public int getEndDay() {
        return endDateTime.getDayOfMonth();
    }
    public int getWeekOfYear() {
        return startDateTime.getWeekOfWeekyear();
    }

    @Override
    public String toString(){
        return this.startDateTime.toString();
    }

    public DateTime getStartDateTime() {
        return startDateTime;
    }

    public void setStartDateTime(DateTime startDateTime) {
        this.startDateTime = startDateTime;
        this.startDate = startDateTime.getMillis();
    }

    public DateTime getEndDateTime() {
        return endDateTime;
    }

    public void setEndDateTime(DateTime endDateTime) {
        this.endDateTime = endDateTime;
        this.endDate = this.endDateTime.getMillis();
    }

    @Override
    public EventPrototype cloneEvent() {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(this);
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            Object deepCopy = ois.readObject();
            return (EventPrototype) deepCopy;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}
